#!/usr/bin/make -f

include /usr/share/quilt/quilt.make

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_ARCH      ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

ifeq ($(DEB_HOST_ARCH_OS),linux)
# Note: We differ from Debian here by favoring the pulseaudio driver, since that's default in Ubuntu.
#       Users override this when calling qemu by exporting the environment variable QEMU_AUDIO_DRV.
conf_arch += --audio-drv-list="pa,alsa,sdl,oss"
endif
ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
conf_arch += --audio-drv-list=oss,sdl,pa
endif
ifeq ($(DEB_HOST_ARCH),powerpc)
conf_arch += --disable-kvm
endif

debsrc_ver := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p' | perl -pe 's/-[^-]+$$//o')
debian_rev := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p' | perl -pe 's/.*-//o')

STATIC_BUILDDIR := static-build

CFLAGS += -Wall -g -O$(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),0,2)

# binfmt support
ALL_BINFMT_TARGETS := alpha arm armeb cris i386 m68k microblaze mips mipsel ppc ppc64 ppc64abi32 sh4 sh4eb sparc sparc32plus sparc64 x86_64
ifeq ($(DEB_HOST_ARCH),amd64)
BINFMT_TARGETS := $(filter-out i386 x86_64, $(ALL_BINFMT_TARGETS))
endif
ifneq (,$(findstring $(DEB_HOST_ARCH), arm armel))
BINFMT_TARGETS := $(filter-out arm, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),i386)
BINFMT_TARGETS := $(filter-out i386, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),powerpc)
BINFMT_TARGETS := $(filter-out ppc, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),ppc64)
BINFMT_TARGETS := $(filter-out ppc ppc64 ppc64abi32 , $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),sparc)
BINFMT_TARGETS := $(filter-out sparc sparc32plus sparc64, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),sparc64)
BINFMT_TARGETS := $(filter-out sparc sparc32plus sparc64, $(ALL_BINFMT_TARGETS))
endif
BINFMT_TARGETS ?= $(ALL_BINFMT_TARGETS)

ifeq ($(filter qemu-kvm-extras-static,$(shell dh_listpackages -s)),)
DO_STATIC := no
MAYBE_STATIC_CONFIG :=
else
DO_STATIC := yes
MAYBE_STATIC_CONFIG := $(STATIC_BUILDDIR)/config-host.mak
endif

ifeq ($(filter qemu-kvm,$(shell dh_listpackages -s)),)
DO_KVM := no
else
DO_KVM := yes
endif

config-host.mak: $(QUILT_STAMPFN)
	dh_testdir
	./configure \
	    --target-list="" \
	    --prefix=/usr \
	    --interp-prefix=/etc/qemu-binfmt/%M \
	    --disable-blobs \
	    --disable-strip \
	    $(conf_arch)

$(STATIC_BUILDDIR)/config-host.mak: $(QUILT_STAMPFN)
	dh_testdir
	rm -rf $(STATIC_BUILDDIR)
	mkdir -p $(STATIC_BUILDDIR)
	#cp -a $(filter-out config-host.mak debian $(STATIC_BUILDDIR),$(wildcard *)) $(STATIC_BUILDDIR)
	cd $(STATIC_BUILDDIR) && \
	    ../configure \
	        --target-list="" \
	        --prefix=/usr \
	        --interp-prefix=/etc/qemu-binfmt/%M \
	        --disable-blobs \
	        --disable-strip \
	        --disable-system \
	        --static \
	        $(conf_arch)

build: build-stamp
build-stamp: config-host.mak $(MAYBE_STATIC_CONFIG)
	dh_testdir
ifeq ($(DO_STATIC),yes)
	$(MAKE) -C $(STATIC_BUILDDIR)
endif
	touch $@

clean:
	dh_testdir
	rm -f install-stamp build-stamp
ifeq ($(DO_STATIC),yes)
	rm -rf $(STATIC_BUILDDIR)
endif
	[ ! -f config-host.mak ] || $(MAKE) distclean
	$(MAKE) -f debian/rules unpatch
	rm -f kvm/extboot/*.o kvm/extboot/extboot.img  kvm/extboot/signrom config.mak kvm/user/config.mak kvm/user/test/lib/.*.d kvm/user/test/lib/*/.*.d kvm/bios/acpi-dsdt.aml kvm/bios/acpi-ssdt.aml qemu-monitor.texi
	dh_clean

install: install-stamp
install-stamp: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	# Build & install normally
ifeq ($(DO_KVM),yes)
	$(MAKE) -C kvm/libkvm DESTDIR=$(CURDIR)/debian/qemu-kvm install
endif
	$(MAKE) DESTDIR=$(CURDIR)/debian/qemu-kvm install
	# Move the extras binaries into place
	for i in alpha arm armeb cris m68k microblaze mips mipsel ppc ppc64 ppc64abi32 sh4 sh4eb sparc sparc32plus sparc64 system-arm system-cris system-m68k system-microblaze system-mips system-mips64 system-mips64el system-mipsel system-ppc system-ppc64 system-ppcemb system-sh4 system-sh4eb system-sparc system-sparc64; do \
		mv debian/qemu-kvm/usr/bin/qemu-$$i debian/qemu-kvm-extras/usr/bin/qemu-$$i; \
	done
ifeq ($(DO_STATIC),yes)
	for target in $(STATIC_BUILDDIR)/*-*-user/qemu-*; do \
	    install -m 755 $$target debian/qemu-kvm-extras-static/usr/bin/$$(basename "$$target")-static; \
	done
	for target in $(BINFMT_TARGETS); do \
	    install -m 644 $(CURDIR)/debian/binfmts/qemu-$$target \
	        $(CURDIR)/debian/qemu-kvm-extras-static/usr/share/binfmts; \
	done
	cp debian/qemu-debootstrap debian/qemu-kvm-extras-static/usr/bin/
	ln -s qemu-debootstrap debian/qemu-kvm-extras-static/usr/bin/build-arm-chroot
	f=debian/qemu-kvm-extras-static.sysctl; [ ! -f debian/qemu-kvm-extras-static.sysctl.$(DEB_BUILD_ARCH) ] || f=debian/qemu-kvm-extras-static.sysctl.$(DEB_BUILD_ARCH); cp $$f debian/qemu-kvm-extras-static/etc/sysctl.d/30-qemu-kvm-extras-static.conf
endif
	touch $@

binary-indep:
	dh_testdir
	dh_testroot
	$(MAKE) -C pc-bios/optionrom
	dh_install -i
	dh_installdocs -X.bzr -i
	dh_installman -i
	dh_installchangelogs -i
	dh_installdebconf -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installdocs -s debian/changelog.qemu debian/changelog.kvm
	dh_installinit -s --no-restart-on-upgrade --error-handler=true
	dh_installman -s
	dh_installudev -s --priority=45
	dh_installchangelogs -s
	dh_install -s
	dh_strip -s
	dh_link -s
	dh_compress -s
	dh_fixperms -s
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s -Nkvm
	dh_gencontrol -pkvm -- -v1:84+dfsg-0ubuntu16+$(debsrc_ver)+$(debian_rev)
ifeq ($(DO_STATIC),yes)
	sed -i 's/@BINFMT_TARGETS@/$(BINFMT_TARGETS)/g' debian/qemu-kvm-extras-static/DEBIAN/*
endif
	# Prune keymaps from qemu-kvm, as these are now in qemu-common
	rm -rf debian/qemu-kvm/usr/share/qemu/keymaps
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install
